#include "display.h"
#include "config.h"
#include "hal/hal.h"

#include "hardware/gpio.h"

#include "FreeRTOS.h"
#include "task.h"

#include "lvgl/examples/lv_examples.h"


extern void vApplicationTickHook();


void vApplicationTickHook()
{
	lv_tick_inc(5);
}


#if 1
TaskHandle_t handleTaskLvgl;

void TaskLvglUpdate(void* parameter)
{
    TickType_t xLastWakeTime;
	
	xLastWakeTime = xTaskGetTickCount();  
	
	for(;;) {		
		vTaskDelayUntil( &xLastWakeTime,pdMS_TO_TICKS( 20 ) );
		lv_task_handler();
	}
	vTaskDelete(NULL);
    
}

static portTASK_FUNCTION(led_task_handler, pvParameters)
{
	TickType_t xLastWakeTime;
	
	xLastWakeTime = xTaskGetTickCount();  
	
	for(;;) {		
		vTaskDelay(pdMS_TO_TICKS( 500 ) );
		gpio_put(PICO_DEFAULT_LED_PIN, 1);
        vTaskDelay(pdMS_TO_TICKS( 500 ) );
		gpio_put(PICO_DEFAULT_LED_PIN, 0);
	}
	vTaskDelete(NULL);
}

void App_PortInit(void)
{
    
    lv_init();
    lv_port_disp_init();
    lv_port_indev_init();


    // Update display in parallel thread.
    xTaskCreate(
        TaskLvglUpdate,
        "LvglThread",
        20480,
        nullptr,
        configMAX_PRIORITIES - 1,
        &handleTaskLvgl);

     vTaskCoreAffinitySet(handleTaskLvgl, (1 << 0));

    TaskHandle_t led_task_handle;
    xTaskCreate(led_task_handler, "led_task", 1048, NULL, (tskIDLE_PRIORITY + 3), &led_task_handle);
    vTaskCoreAffinitySet(led_task_handle, (1 << 1));



}

void App_PortStart(void)
{
    vTaskStartScheduler();    
}
#endif